<?php
// require_once(dirname(__FILE__) . '/datatypes/Datatype_base.php');
// require_once(dirname(__FILE__) . '/datatypes/Datatype_field.php');
// require_once(dirname(__FILE__) . '/datatypes/Datatype_abstract.php');
// require_once(dirname(__FILE__) . '/datatypes/Datatype_field_numeric.php');
// require_once(dirname(__FILE__) . '/datatypes/Datatype_real.php');

/**
 * Static datatype helper'n convenience class.
 *
 * @package EDO
 * @subpackage Datatypes
 * 
 */
class Lead_EDO_Datatype_Factory
{

	
	/**
	 * Datatype factory. Returns a datatype object.
	 *
	 * @param string $datatype Datatype name
	 * @param array $configArr Config array
	 * @deprecated 
	 * 
	 */
	public static function factory(Lead_EDO_Object_Abstract $owner, $datatype, $configArr = array())
	{
		if ($datatype == 'real')
		{
		    throw new Exception('Except!');
		}
	    
	    // $datatype = strtolower($datatype);
	    $className = self::load($datatype);
		
        $datatypeObj = new $className($owner, $configArr);
                        
        if(!$datatypeObj)
            throw new Exception('lalala');
        
        return $datatypeObj;
	   
	}
	
	
	/**
	 * Loads a datatype.
	 *
	 * @param string $datatype Datatype name
	 * @return string Datatypes class name.
	 */
	public static function load($datatype)
	{
   		// print $datatype;
	    
	    $datatype = ucfirst($datatype);
	    $className = "Lead_EDO_Datatype_{$datatype}";
		Zend::loadClass($className);
   		
   		// require_once dirname(__FILE__)."/datatypes/{$className}.php";
		return $className;
	}
	
	
	
	/**
	 * Lists all datatypes.
	 *
	 * @todo This won't work like this for long...
	 * @return array An array of all available datatypes
	 * @deprecated
	 * 
	 */
	public static function listAll()
	{
		$dirName = dirname(__FILE__)."/datatypes/";

		if (is_dir($dirName)) 
		{
   			if ($dh = opendir($dirName)) 
   			{
		       while (($file = readdir($dh)) !== false) 
		       {
		           if(is_file($dirName . $file))
		           {
		           		$pattern = "/^Datatype_(.*?)\.php/";
		           		$matches = array();
		           		preg_match($pattern, $file, $matches);
						$dataTypes[] = $matches[1];
		           }
		       }
		       closedir($dh);
		   }
		}
		return $dataTypes;
	}
}